package com.o2opuzi.lib.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class FieldErrorUtils {
	
	public static Map<String, Object> getErrors(BindingResult result){
		Field[] fields = result.getTarget().getClass().getDeclaredFields();
		List<FieldError> errors = result.getFieldErrors();
		Map<String, Object> errMap  = new LinkedHashMap<String, Object>();
		for (Field field : fields) {
			for (FieldError fieldError : errors) {
				if(field.getName().equals(fieldError.getField())){
					errMap.put(fieldError.getField(),fieldError.getDefaultMessage());
					break;
				}
			}
		}
		return errMap;
	}
}
